var HashMap = require('./HashMap')
/**
 * 使用说明：
 * 1、引入 var  PKGame =require("../utils/PKGame.js")
 * 2、onload 里面实例化并调用start方法：
 *     	mTimerTools = new TimerTools(this);
			mTimerTools.start();

添加监听函数：keykey随意写不要重复就好
 mTimerTools.addCallBack("keykey", () => {})
 */
//

class TimerTools {
	constructor(handler) {
		this.mHandler = handler;
		this.mHandler;
		this.commonTimer;
		this.callBackListener = new HashMap();
		this.instance;
		// uni.setStorageSync("token",handler.appInfo.token)
	}

	destroy() {
		clearInterval(this.commonTimer)
		this.commonTimer = null;
	}
	start() {
		if (!this.commonTimer) {
			this.commonTimer = setInterval(() => {
				var values = this.callBackListener.values();
				for (var i in values) {
					typeof values[i] == "function" && values[i]();
				}
			}, 5000);
		}
		// this.createRoom();
	}
	
	addCallBack(listenerKey, listener) {
		this.callBackListener.put(listenerKey, listener)
	}
	removeCallBack(listenerKey) {
		this.callBackListener.remove(listenerKey)
	}

	static getInstance = function (handler) { //静态方法
    return this.instance || (this.instance = new TimerTools(handler))
  }
}

module.exports = ( handler) => {
	return TimerTools.getInstance( handler)
};
